package com.laf.manager.controller;

import com.laf.manager.schedule.ScheduleConfig;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

/**
 * @author ygl
 * @create 2019-01-09
 * @DESCRIPTION
 **/
@Slf4j
@RestController
@RequestMapping("/test")
public class TestController {

    @Autowired
    private ScheduleConfig scheduleConfig;

    @RequestMapping("/start/{minute}")
    public String payFee(@PathVariable int minute) {
        String cron = "0 0/"+minute+" * * * ? ";
        if(!StringUtils.isEmpty(cron)) {
            scheduleConfig.setCron(cron);
            log.info("开启定时任务成功"+cron);
            return "开启定时任务成功";
        } else {
            log.error("开启定时任务失败");
            return "开启定时任务失败";
        }
    }

    /**
     * （关闭定时任务）
     */
    @RequestMapping("/close")
    public String closePayFee() {
        try {
            scheduleConfig.stopCron();
            log.info("关闭定时任务成功");
            return "关闭定时任务成功";
        }catch (Exception e){
            log.error("关闭定时任务失败");
        }
        return "关闭定时任务失败";
    }
}
